package com.wn.ziroom.controller;


import com.wn.ziroom.entity.OrderRoom;
import com.wn.ziroom.entity.User;
import com.wn.ziroom.service.OrderRoomService;
import com.wn.ziroom.utils.CookieUtils;
import com.wn.ziroom.utils.JwtUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-26 05:45:34
 */
@RestController
@RequestMapping("/orderRoom")
public class OrderRoomController {

    @Autowired
    private OrderRoomService orderRoomService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/addOrder")
    public String addOrder(@DateTimeFormat(pattern = "yyyy-MM-dd") Date time,Integer roomId, HttpServletRequest request) {
        String name = "token";
        String token = CookieUtils.getCookie(name, request);
        User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);

        OrderRoom orderRoom = new OrderRoom();
        orderRoom.setTime(time);
        orderRoom.setUserId(user.getId());
        orderRoom.setRoomId(roomId);
        orderRoomService.save(orderRoom);
//        long orderTime = orderRoom.getTime().getTime();
//
//        long now = new Date().getTime();
//
//        String ttl =  String.valueOf(orderTime-now);
//        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setExpiration(ttl);//设置单个消息的过期时间
//        Message message = new Message(String.valueOf(orderRoom.getId()).getBytes(), messageProperties);
//        rabbitTemplate.convertAndSend("room_normal_exchange","room_normal_routing_key", message);

        return "ok";
    }

}

